package haoyou.csnf.manager.module.pm.service.employeeinfo;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import haoyou.csnf.manager.module.pm.controller.admin.employeeinfo.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.employeeinfo.EmployeeInfoDO;
import haoyou.csnf.manager.module.pm.dal.dataobject.employeeeducations.EmployeeEducationsDO;
import haoyou.csnf.manager.module.pm.dal.dataobject.employeetitles.EmployeeTitlesDO;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;

import haoyou.csnf.manager.module.pm.dal.mysql.employeeinfo.EmployeeInfoMapper;
import haoyou.csnf.manager.module.pm.dal.mysql.employeeeducations.EmployeeEducationsMapper;
import haoyou.csnf.manager.module.pm.dal.mysql.employeetitles.EmployeeTitlesMapper;

import static haoyou.csnf.manager.framework.common.exception.util.ServiceExceptionUtil.exception;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.convertList;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.diffList;
import static haoyou.csnf.manager.module.pm.enums.ErrorCodeConstants.*;

/**
 * 职工信息 Service 实现类
 *
 * @author 好有经验源码
 */
@Service
@Validated
public class EmployeeInfoServiceImpl implements EmployeeInfoService {

    @Resource
    private EmployeeInfoMapper employeeInfoMapper;
    @Resource
    private EmployeeEducationsMapper employeeEducationsMapper;
    @Resource
    private EmployeeTitlesMapper employeeTitlesMapper;

    @Override
    public Long createEmployeeInfo(EmployeeInfoSaveReqVO createReqVO) {
        // 插入
        EmployeeInfoDO employeeInfo = BeanUtils.toBean(createReqVO, EmployeeInfoDO.class);
        employeeInfoMapper.insert(employeeInfo);
        // 返回
        return employeeInfo.getId();
    }

    @Override
    public void updateEmployeeInfo(EmployeeInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateEmployeeInfoExists(updateReqVO.getId());
        // 更新
        EmployeeInfoDO updateObj = BeanUtils.toBean(updateReqVO, EmployeeInfoDO.class);
        employeeInfoMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEmployeeInfo(Long id) {
        // 校验存在
        validateEmployeeInfoExists(id);
        // 删除
        employeeInfoMapper.deleteById(id);

        // 删除子表
        deleteEmployeeEducationsByEmpId(id);
        deleteEmployeeTitlesByEmpId(id);
    }

    @Override
        @Transactional(rollbackFor = Exception.class)
    public void deleteEmployeeInfoListByIds(List<Long> ids) {
        // 校验存在
        validateEmployeeInfoExists(ids);
        // 删除
        employeeInfoMapper.deleteByIds(ids);
    
    // 删除子表
            deleteEmployeeEducationsByEmpIds(ids);
            deleteEmployeeTitlesByEmpIds(ids);
    }

    private void validateEmployeeInfoExists(List<Long> ids) {
        List<EmployeeInfoDO> list = employeeInfoMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(EMPLOYEE_INFO_NOT_EXISTS);
        }
    }

    private void validateEmployeeInfoExists(Long id) {
        if (employeeInfoMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public EmployeeInfoDO getEmployeeInfo(Long id) {
        return employeeInfoMapper.selectById(id);
    }

    @Override
    public PageResult<EmployeeInfoDO> getEmployeeInfoPage(EmployeeInfoPageReqVO pageReqVO) {
        return employeeInfoMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（职工学历） ====================

    @Override
    public PageResult<EmployeeEducationsDO> getEmployeeEducationsPage(PageParam pageReqVO, Integer empId) {
        return employeeEducationsMapper.selectPage(pageReqVO, empId);
    }

    @Override
    public Long createEmployeeEducations(EmployeeEducationsDO employeeEducations) {
        // 校验是否已经存在
        if (employeeEducationsMapper.selectByEmpId(employeeEducations.getEmpId()) != null) {
            throw exception(EMPLOYEE_EDUCATIONS_EXISTS);
        }
        // 插入
        employeeEducationsMapper.insert(employeeEducations);
        return employeeEducations.getId();
    }

    @Override
    public void updateEmployeeEducations(EmployeeEducationsDO employeeEducations) {
        // 校验存在
        validateEmployeeEducationsExists(employeeEducations.getId());
        // 更新
        employeeEducations.clean(); // 解决更新情况下：updateTime 不更新
        employeeEducationsMapper.updateById(employeeEducations);
    }

    @Override
    public void deleteEmployeeEducations(Long id) {
        // 删除
        employeeEducationsMapper.deleteById(id);
    }

	@Override
	public void deleteEmployeeEducationsListByIds(List<Long> ids) {
        // 删除
        employeeEducationsMapper.deleteByIds(ids);
	}

    @Override
    public EmployeeEducationsDO getEmployeeEducations(Long id) {
        return employeeEducationsMapper.selectById(id);
    }

    private void validateEmployeeEducationsExists(Long id) {
        if (employeeEducationsMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_EDUCATIONS_NOT_EXISTS);
        }
    }

    private void deleteEmployeeEducationsByEmpId(Long empId) {
        employeeEducationsMapper.deleteByEmpId(empId);
    }

	private void deleteEmployeeEducationsByEmpIds(List<Long> empIds) {
        employeeEducationsMapper.deleteByEmpIds(empIds);
	}

    // ==================== 子表（职工职称） ====================

    @Override
    public PageResult<EmployeeTitlesDO> getEmployeeTitlesPage(PageParam pageReqVO, Integer empId) {
        return employeeTitlesMapper.selectPage(pageReqVO, empId);
    }

    @Override
    public Long createEmployeeTitles(EmployeeTitlesDO employeeTitles) {
        employeeTitlesMapper.insert(employeeTitles);
        return employeeTitles.getId();
    }

    @Override
    public void updateEmployeeTitles(EmployeeTitlesDO employeeTitles) {
        // 校验存在
        validateEmployeeTitlesExists(employeeTitles.getId());
        // 更新
        employeeTitles.clean(); // 解决更新情况下：updateTime 不更新
        employeeTitlesMapper.updateById(employeeTitles);
    }

    @Override
    public void deleteEmployeeTitles(Long id) {
        // 删除
        employeeTitlesMapper.deleteById(id);
    }

	@Override
	public void deleteEmployeeTitlesListByIds(List<Long> ids) {
        // 删除
        employeeTitlesMapper.deleteByIds(ids);
	}

    @Override
    public EmployeeTitlesDO getEmployeeTitles(Long id) {
        return employeeTitlesMapper.selectById(id);
    }

    private void validateEmployeeTitlesExists(Long id) {
        if (employeeTitlesMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_TITLES_NOT_EXISTS);
        }
    }

    private void deleteEmployeeTitlesByEmpId(Long empId) {
        employeeTitlesMapper.deleteByEmpId(empId);
    }

	private void deleteEmployeeTitlesByEmpIds(List<Long> empIds) {
        employeeTitlesMapper.deleteByEmpIds(empIds);
	}

}